﻿using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT.Net.NetworkInformation;

namespace NetworkTest
{
    public partial class Program
    {
        GT.Networking.WebEvent setEvent;
        GT.Networking.WebEvent getEvent;

        double value01 = 0;
        double value02 = 0;
        double value03 = 0;

        void ProgramStarted()
        {
            //ethernet.UseDHCP();
            ethernet.UseStaticIP(
                "169.254.124.219",
                "255.255.0.0",
                "169.254.0.1");
            ethernet.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);
            ethernet.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkDown);
            
            Debug.Print("Starting...");
            multicolorLed1.BlinkRepeatedly(GT.Color.Yellow);
            multicolorLed2.BlinkRepeatedly(GT.Color.Magenta);
            multicolorLed3.BlinkRepeatedly(GT.Color.Cyan);

        }
        
        void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network up");
            string ipAddress = ethernet.NetworkSettings.IPAddress;
            WebServer.StartLocalServer(ipAddress, 8080);
            Debug.Print(ipAddress);
            setEvent = WebServer.SetupWebEvent("set");
            getEvent = WebServer.SetupWebEvent("get");
            setEvent.WebEventReceived += new WebEvent.ReceivedWebEventHandler(setEvent_WebEventReceived);
            getEvent.WebEventReceived += new WebEvent.ReceivedWebEventHandler(getEvent_WebEventReceived);

            multicolorLed1.TurnOff();
            multicolorLed2.TurnOff();
            multicolorLed3.TurnOff();
        }

        void setEvent_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            Debug.Print("Set Event");
            string content = "<html><body><h1>OK</h1></body></html>";
            byte[] bytes = new System.Text.UTF8Encoding().GetBytes(content);

            if (responder.UrlParameters.Contains("id") && responder.UrlParameters.Contains("value"))
            {
                double value = Double.Parse(responder.UrlParameters["value"].ToString());
                
                switch(responder.UrlParameters["id"].ToString()) {
                    case "01":
                        value01 = value;
                        break;
                    case "02":
                        value02 = value;
                        break;
                    case "03":
                        value03 = value;
                        break;
                }
            }

            Debug.Print("Blue: " + (byte)(value01 * 255));
            Debug.Print("Green: " + (byte)(value02 * 255));
            Debug.Print("Red: " + (byte)(value03 * 255));
            multicolorLed1.SetBlueIntensity((byte)(value01 * 255));
            multicolorLed2.SetGreenIntensity((byte)(value02 * 255));
            multicolorLed3.SetRedIntensity((byte)(value03 * 255));

            responder.Respond(bytes, "text/html");
        }

        void getEvent_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            Debug.Print("Get Event");
            string content = "";
            content += 
                "[" +
                "{" +
                    "id:\"01\"," + 
                    "value:" + value01 +
                "}," +
                "{" +
                    "id:\"02\"," + 
                    "value:" + value02 +
                "}," +
                "{" +
                    "id:\"03\"," + 
                    "value:" + value03 +
                "}" +
                "]";

            byte[] bytes = new System.Text.UTF8Encoding().GetBytes(content);
            responder.Respond(bytes, "text/html");
        }

        void ethernet_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network down");
            multicolorLed1.BlinkRepeatedly(GT.Color.Purple);
            multicolorLed2.BlinkRepeatedly(GT.Color.Purple);
            multicolorLed3.BlinkRepeatedly(GT.Color.Purple);
        }

    }

}
